#include "array.h"

// ---------------- CONSTRUCTOR & DESTRUCTOR ------------------------------------
Array::Array(char * fileName){
    this->fileName = fileName;
    this->cnt = 0;
    this->array = readInput();
}

Array::~Array(){
    clearArray();
}

// ---------------- PRIVATE METHODS ---------------------------------------------
/**
 * Read the input file.
 */
int * Array::readInput(){
    ifstream file(fileName);

    if (file.is_open()){
        // Read items count
        string line;
        getline(file, line);
        unsigned long count = atoi(line.c_str());
        int * array = new int[count];

        // Read items and fill array
        getline(file, line);
        string numStrs(line);
        istringstream iss(numStrs);
        string numStr;

        if(file.good()){
            for (unsigned long ii=0;ii<count;ii++){
                getline(iss, numStr, ' ');
                array[ii] = atoi(numStr.c_str());
            }
        }

        file.close();
        cnt = count;

        return array;
    }
    return NULL;
}

/**
 * Clear the array.
 */
void Array::clearArray(){
    delete [] array;
}

// ---------------- PUBLIC METHODS ---------------------------------------------
/**
 * Print the array
 */
void Array::printArray(){
    for (unsigned long i = 0; i < cnt; i++){
        cout << array[i] << " ";
    }
    cout << endl;
}

int * Array::getArray(){
    return array;
}

unsigned long Array::getCnt(){
    return cnt;
}
